/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-15 04:21:11
 */
/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-08 21:18:31
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanRider_Work;
import zyy.util.BaseException;
import zyy.util.Bus;
import zyy.util.BusinessException;

public class FrmRiderWork extends JDialog implements ActionListener {
  private static final long serialVersionUID = -7010944395384654344L;

  private BeanRider_Work selectRiderWork = null;
  private Object tblRiderWorkTitles[] = BeanRider_Work.tblTitles;
  private Object tblRiderWorkData[][];
  DefaultTableModel tabRiderWorkModel = new DefaultTableModel();
  private JTable dataTableRiderWork = new JTable(tabRiderWorkModel);
  List<BeanRider_Work> allRiderWork = null;

  private JButton exit = new JButton("退出");
  private JButton add100 = new JButton("+200");
  private JButton clear = new JButton("擦除冗余");
  private JPanel btns = new JPanel();

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.exit) {
      this.setVisible(false);
    } else if (e.getSource() == this.add100) {
      try {
        Bus.riderManager.add200();
        this.reload();
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    } else if (e.getSource() == this.clear) {
      try {
        Bus.riderManager.clearAdd();
        this.reload();
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    }
  }

  public void reload() {
    try {
      allRiderWork = Bus.riderManager.loadWorks();
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblRiderWorkData = new Object[allRiderWork.size()][BeanRider_Work.tblTitles.length];
    for (int i = 0; i < allRiderWork.size(); i++) {
      for (int j = 0; j < BeanRider_Work.tblTitles.length; j++)
        tblRiderWorkData[i][j] = allRiderWork.get(i).getCell(j);
    }
    tabRiderWorkModel.setDataVector(tblRiderWorkData, tblRiderWorkTitles);
    // 限定列宽必须在reload里写死
    dataTableRiderWork.getColumnModel().getColumn(0).setPreferredWidth(55);
    dataTableRiderWork.getColumnModel().getColumn(1).setPreferredWidth(65);
    dataTableRiderWork.getColumnModel().getColumn(2).setPreferredWidth(130);
    dataTableRiderWork.getColumnModel().getColumn(3).setPreferredWidth(330);
    dataTableRiderWork.getColumnModel().getColumn(4).setPreferredWidth(330);
    dataTableRiderWork.getColumnModel().getColumn(5).setPreferredWidth(120);
    dataTableRiderWork.getColumnModel().getColumn(6).setPreferredWidth(210);
    dataTableRiderWork.getColumnModel().getColumn(7).setPreferredWidth(80);
    dataTableRiderWork.getColumnModel().getColumn(8).setPreferredWidth(100);
    this.dataTableRiderWork.validate();
    this.dataTableRiderWork.repaint();
  }

  public FrmRiderWork() {
    // JDialog 基础设置
    {
      this.setTitle("外卖助手 骑手 历史订单");
      // this.setLocationRelativeTo(null);
      this.setLocation(75, 170);
      this.setSize(1327, 557);
      this.setVisible(true);
      this.reload();
    }
    // JTable 地址列表
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(new JScrollPane(this.dataTableRiderWork), BorderLayout.CENTER);
      this.dataTableRiderWork.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmRiderWork.this.dataTableRiderWork.getSelectedRow();
          if (i < 0) {
            return;
          }
          {
            // 同步数据更新和UI更新
            FrmRiderWork.this.selectRiderWork = FrmRiderWork.this.allRiderWork.get(i);
            System.out.println(FrmRiderWork.this.selectRiderWork);
            FrmRiderWork.this.reload();
            FrmRiderWork.this.dataTableRiderWork.setRowSelectionInterval(i, i);
          }
        }
      });
    }
    // btns 设置
    {
      btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.getContentPane().add(btns, BorderLayout.SOUTH);
      btns.add(add100);
      btns.add(clear);
      btns.add(exit);
      Bus.jButtonZYYStyle(add100);
      Bus.jButtonZYYStyle(clear);
      Bus.jButtonZYYStyle(exit);
      this.add100.addActionListener(this);
      this.clear.addActionListener(this);
      this.exit.addActionListener(this);
    }
  }
}